package com.v.im.businessservice.service.impl;

import com.v.im.businessapi.dto.resultDTO.ImChatGroupResultDTO;
import com.v.im.businessservice.bean.ImChatGroupEntity;
import com.v.im.businessservice.bean.ImChatGroupUserEntity;
import com.v.im.businessservice.service.base.BaseService;
import com.v.im.businessservice.service.iface.IImChatGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ImChatGroupServiceImpl extends BaseService implements IImChatGroupService {

    @Override
    public List getUserGroups(String userId) {
        List<ImChatGroupResultDTO> resultDTOS = new ArrayList<>();
        List<ImChatGroupUserEntity> imChatGroupUserEntities = imChatGroupUserDao.findByUserId(userId);
        if (!CollectionUtils.isEmpty(imChatGroupUserEntities)) {
            Set groupIds = imChatGroupUserEntities.stream().map(ImChatGroupUserEntity::getChatGroupId).
                    collect(Collectors.toSet());
            List<ImChatGroupEntity> imChatGroupEntities = imChatGroupDao.findByIdIn(groupIds);
            if (!CollectionUtils.isEmpty(imChatGroupEntities)) {
                for (ImChatGroupEntity imChatGroupEntity : imChatGroupEntities) {
                    ImChatGroupResultDTO imChatGroupResultDTO = new ImChatGroupResultDTO();
                    BeanUtils.copyProperties(imChatGroupEntity,imChatGroupResultDTO);
                    resultDTOS.add(imChatGroupResultDTO);
                }
            }
        }
        return resultDTOS;
    }
}
